/*!
 * @file Sensor.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/Game>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
Sensor::Sensor(void):
    m_invert            ( false ),
    m_level             ( false ),
    m_tap               ( false ),

    m_prev              ( false )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Sensor::Sensor(const Sensor& other, const osg::CopyOp& copyop):
    osg::Object             ( other, copyop ),
    m_invert                ( other.m_invert ),
    m_level                 ( other.m_level ),
    m_tap                   ( other.m_tap ),
    m_controller_list       ( other.m_controller_list ),

    m_prev                  ( other.m_prev )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Sensor::~Sensor(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Sensor::operator()( osgODE::ODEObject* object, Game* game )
{
    const bool  active = pulse( object, game ) ;


    const bool  reset = m_prev & active & m_tap ;
    const bool  level = (m_prev ^ active) & m_level ;

    m_prev = active ;



    bool    positive = ( active & !m_level )  |  level  ;

    positive        ^= reset ;
    positive        ^= m_invert ;






    notifyControllers( game->getControllerList(), positive ) ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Sensor::notifyControllers( ControllerList& controllers, bool value )
{
    std::vector<unsigned int>::const_iterator   itr = m_controller_list.begin() ;
    std::vector<unsigned int>::const_iterator   itr_end = m_controller_list.end() ;

    while( itr != itr_end ) {

        controllers[ *itr++ ]->accumulate( value ) ;
    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
bool
Sensor::pulse( osgODE::ODEObject* object, Game* game )
{
    (void) object ;
    (void) game ;

    return false ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
MessageSensor*
Sensor::asMessageSensor(void)
{
    return NULL ;
}
/* ....................................................................... */
/* ======================================================================= */
